package com.lykj.modules.shop.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lykj.core.boot.ctrl.LykjController;
import com.lykj.core.mp.support.Condition;
import com.lykj.core.mp.support.Query;
import com.lykj.core.secure.utils.AuthUtil;
import com.lykj.core.tool.api.R;
import com.lykj.modules.shop.entity.ServerArea;
import com.lykj.modules.shop.mapper.ServerAreaMapper;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.lykj.modules.shop.vo.ServerAreaVo;
import com.lykj.modules.shop.service.ServerAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 区域服务商Controller
 *
 * @author ruoyi
 * @date 2022-08-14
 */
@Validated
@Api(value = "区域服务商控制器", tags = {"区域服务商管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/shop/serverArea")
public class ServerAreaController extends LykjController {

    private final ServerAreaService serverAreaService;
    private final ServerAreaMapper serverAreaMapper;


    @GetMapping("/list")
    public R<IPage<ServerArea>> list(@ApiIgnore @RequestParam Map<String, Object> map, Query query) {
        if (query.getAscs() == null && query.getDescs() == null) {
            query.setDescs("id");
        }
        ServerArea userBill = JSON.parseObject(JSON.toJSONString(map), ServerArea.class);
        IPage<ServerArea> pages = serverAreaService.queryPageList(Condition.getPage(query), userBill);
        return R.data(pages);
    }




    /**
     * 获取区域服务商详细信息
     */
    @ApiOperation("获取区域服务商详细信息")
    @GetMapping("/{id}")
    public R<ServerArea> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.data(serverAreaService.queryById(id));
    }

    @ApiOperation("根据用户ID获取区域服务商详细信息")
    @GetMapping("/get/{userId}")
    public R<ServerAreaVo> getInfoByUserId(@ApiParam("主键")
                                 @NotNull(message = "主键不能为空")
                                 @PathVariable("userId") Long userId) {
        return R.data(serverAreaService.queryByUserId(userId));
    }

    /**
     * 变更区域服务商
     */
    @ApiOperation("变更区域服务商")
    @PostMapping("/change")
    public R<Void> addOrUpd(@RequestBody ServerArea bo) {
        bo.setIsConfirm(0);
        if (BeanUtil.isNotEmpty(bo.getId())) {
            return R.status(serverAreaService.updateByBo(bo));
        }
        bo.setUserId(AuthUtil.getUserId());
        return R.status(serverAreaService.insertByBo(bo));
    }

    /**
     * 新增区域服务商
     */
    @ApiOperation("新增区域服务商")
    @PostMapping()
    public R<Void> add(@RequestBody ServerArea bo) {
        return R.status(serverAreaService.insertByBo(bo));
    }

    /**
     * 修改区域服务商
     */
    @ApiOperation("修改区域服务商")
    @PutMapping()
    public R<Void> edit(@RequestBody ServerArea bo) {
        return R.status(serverAreaService.updateByBo(bo));
    }

    /**
     * 删除区域服务商
     */
    @ApiOperation("删除区域服务商")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return R.status(serverAreaService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
